﻿using DAL;
using MD5Hash;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using Model;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;


namespace WebAi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class MemberController : ControllerBase
    {
        public MemberController(Member_DAL dal,ILogger<MemberController> logger)
        {
            Dal = dal;
            Logger = logger;
        }

        public Member_DAL Dal { get; }
        public ILogger<MemberController> Logger { get; }

        /// <summary>
        /// 会员登录
        /// </summary>
        /// <param name="Name">用户名</param>
        /// <param name="Pwd">密码</param>
        /// <returns></returns>
        [HttpGet("Login")]
        public IActionResult Login(string? Name, string? Pwd)
        {
            Logger.LogInformation("开始登陆");
            Member_Model m=Dal.Login(Name, Pwd);
            return Ok(new
            {
                data = m
            });
        }
        /// <summary>
        /// 会员列表
        /// </summary>
        /// <param name="Name">根据会员姓名（模糊查询）</param>
        /// <param name="Age">根据年龄查询</param>
        /// <returns></returns>
        [HttpGet("MemberShow")]
        public IActionResult MemberShow(string? Name, int Age)
        {
           List< Member_Model> m =Dal.MemberShow(Name,Age);
            return Ok(new
            {
                data = m
            });
        }

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="id">选中的Id</param>
        /// <param name="ZT">状态</param>
        /// <returns></returns>
        [HttpGet("XiouGaiZT")]
        public IActionResult XiouGaiZT(int id, string ZT)
        {
           var m=Dal.XiouGaiZT(id, ZT);
            return Ok(new
            {
                data = m
            });
        }

        [HttpGet("DR")]
        public IActionResult DR(string? Name, int Age)
        {
            List<Member_Model> list= Dal.MemberShow(Name, Age);
            IWorkbook Wook=new XSSFWorkbook();
            ISheet sheet = Wook.CreateSheet("房型列表");
            IRow row=sheet.CreateRow(0);
            row.CreateCell(0).SetCellValue("会员名称");
            row.CreateCell(1).SetCellValue("密码");
            row.CreateCell(2).SetCellValue("手机号");
            row.CreateCell(3).SetCellValue("身份证");
            row.CreateCell(4).SetCellValue("出生日期");
            row.CreateCell(5).SetCellValue("年龄");
            row.CreateCell(6).SetCellValue("头像");
            row.CreateCell(7).SetCellValue("状态");

            int index = 1;
            foreach (var item in list)
            {
                row = sheet.CreateRow(index);
                row.CreateCell(0).SetCellValue(item.Name);
                row.CreateCell(1).SetCellValue(item.Pwd);
                row.CreateCell(2).SetCellValue(item.Phone);
                row.CreateCell(3).SetCellValue(item.SZF);
                row.CreateCell(4).SetCellValue(item.CSRQ);
                row.CreateCell(5).SetCellValue(item.Age==1);
                row.CreateCell(6).SetCellValue(item.TX);
                row.CreateCell(7).SetCellValue(item.ZT);
                index++;
            }

            byte[] by;
            using(MemoryStream ms=new MemoryStream())
            {
                Wook.Write(ms);
                by = ms.ToArray();
            }
            string Count = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
            return File(by, Count, "房型列表.xlsx");

        }



    }
}
